import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import {
  SETUSERINFO
} from "@/store/mutations-type";
import {
  getUserByToken
} from '@/request/httpApi'

Vue.use(VueRouter)

const routes = [{
  path: '/login',
  component: () => import( /* webpackChunkName: "login" */ '../views/login/Login.vue')
}]

const pageRoutes = {
  '/good': {
    path: '/good',
    meta: {
      name: '/good'
    },
    component: () => import( /* webpackChunkName: "good" */ '../views/good/Good.vue')
  },
  '/editgood': {
    path: '/editgood/:gid',
    meta: {
      name: '/good'
    },
    component: () => import( /* webpackChunkName: "editgood" */ '../views/good/EditGood.vue')
  },
  '/addgood': {
    path: '/addgood',
    meta: {
      name: '/good'
    },
    component: () => import( /* webpackChunkName: "addgood" */ '../views/good/AddGood.vue')
  },
  '/brand': {
    path: '/brand',
    meta: {
      name: '/brand'
    },
    component: () => import( /* webpackChunkName: "brand" */ '../views/brand/Brand.vue')
  },
  '/editbrand': {
    path: '/editbrand/:bid',
    meta: {
      name: '/brand'
    },
    component: () => import( /* webpackChunkName: "editbrand" */ '../views/brand/EditBrand.vue')
  },
  '/addbrand': {
    path: '/addbrand',
    meta: {
      name: '/brand'
    },
    component: () => import( /* webpackChunkName: "addbrand" */ '../views/brand/AddBrand.vue')
  },
  '/category': {
    path: '/category',
    meta: {
      name: '/category'
    },
    component: () => import( /* webpackChunkName: "category" */ '../views/category/Category.vue')
  },
  '/addcategory': {
    path: '/addcategory',
    meta: {
      name: '/category'
    },
    component: () => import( /* webpackChunkName: "addcategory" */ '../views/category/Addcategory.vue')
  },
  '/editcategory': {
    path: '/editcategory/:cid',
    meta: {
      name: '/category'
    },
    component: () => import( /* webpackChunkName: "editcategory" */ '../views/category/EditCategory.vue')
  },
  '/addattribute': {
    path: '/addattribute',
    meta: {
      name: '/attribute'
    },
    component: () => import( /* webpackChunkName: "addattribute" */ '../views/attribute/AddAttribute.vue')
  },
  '/editattribute': {
    path: '/editattribute/:aid',
    meta: {
      name: '/attribute'
    },
    component: () => import( /* webpackChunkName: "editattribute" */ '../views/attribute/EditAttribute.vue')
  },
  '/order': {
    path: '/order',
    meta: {
      name: '/order'
    },
    component: () => import( /* webpackChunkName: "order" */ '../views/order/Order.vue')
  },
  '/topic': {
    path: '/topic',
    meta: {
      name: '/topic'
    },
    component: () => import( /* webpackChunkName: "topic" */ '../views/topic/Topic.vue')
  },
  '/edittopic': {
    path: '/edittopic/:tid',
    meta: {
      name: '/topic'
    },
    component: () => import( /* webpackChunkName: "edittopic" */ '../views/topic/EditTopic.vue')
  },
  '/addtopic': {
    path: '/addtopic',
    meta: {
      name: '/topic'
    },
    component: () => import( /* webpackChunkName: "addtopic" */ '../views/topic/AddTopic.vue')
  },
  '/user': {
    path: '/user',
    meta: {
      name: '/user'
    },
    component: () => import( /* webpackChunkName: "user" */ '../views/user/User.vue')
  },
  '/attribute': {
    path: '/attribute',
    meta: {
      name: '/attribute'
    },
    component: () => import( /* webpackChunkName: "attribute" */ '../views/attribute/Attribute.vue')
  },
  '/statdata': {
    path: '/statdata',
    meta: {
      name: '/statdata'
    },
    component: () => import( /* webpackChunkName: "statdata" */ '../views/statdata/Statdata.vue')
  }
}

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

const genRoutes = () => {
  let url = '';
  const menu = store.state.menu.reduce((prev, current) => {
    if (!url) {
      url = current.path
    }
    prev.push(pageRoutes[current.path])
    return prev
  }, []);
  const HomeRoute = [{
    path: '/',
    component: () => import( /* webpackChunkName: "homepage" */ '../views/homepage/Homepage.vue'),
    children: menu
  },{
    path: '*',
    redirect: '/good'
  }]
  router.addRoutes(HomeRoute)
  return url
}



router.beforeEach((to, from, next) => {
  if (from.path === '/login' && to.path === '/') {
    const url = genRoutes();
    console.log(url)
    return next(url);
  } else if (from.path != '/login' && localStorage.getItem('token') && !store.state.userinfo) {
    getUserByToken().then(res => {
      if (res.errno === 0) {
        store.commit(SETUSERINFO, res.data);
        localStorage.setItem("token", res.data.token);
        genRoutes();
        next(to)
      }
    })
  } else if (!localStorage.getItem('token') && to.path !== '/login') {
    return next('/login')
  } else {
    next();
  }

})


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router